#include "StdAfx.h"
#include <MyFC/Utility.h>
#include <MyControls/DisplayPanel.h>
#include <MyControls/Frame.h>
#include <MyControls/Constants.h>
#include <MyControls/App.h>
#include <MyControls/Resource.h>
#include <MyViews/ZoomView.h>
#include <sstream>

namespace My {

BackgroundColorBox::BackgroundColorBox()
	: m_pView(0)
{
	SetEnabled(false);
}

String BackgroundColorBox::Hint() const
{
	return GetApp().LoadString(IDS_BACKGROUND);
}

void BackgroundColorBox::SetView(ZoomView * pView)
{
	if (pView != m_pView) {
		m_pView = pView;
		SetEnabled(m_pView != 0);
		Invalidate();
	}
}

bool BackgroundColorBox::GetColor(COLORREF & c) const
{
	if (m_pView == 0)
		return false;
	c = m_pView->GetBackgroundColor();
	return true;
}

void BackgroundColorBox::SetColor(COLORREF c)
{
	if (m_pView != 0)
		m_pView->SetBackgroundColor(c);
}

// DisplayPanel

DisplayPanel::DisplayPanel(Frame & frame) : 
	m_Frame(frame), 
	m_pView(0)
{
	m_Frame.Add(this);
	Add(&m_ZoomEdit);
	Add(&m_ZoomOut);
	Add(&m_ZoomSlider);
	Add(&m_ZoomIn);
	Add(&m_Background);
	m_Background.SetAlignment(ALIGN_CENTER);
}

ZoomView * DisplayPanel::GetView() const
{
	return m_pView;
}

void DisplayPanel::SetView(ZoomView * pView)
{
	if (m_pView != pView)
	{
		m_pView = pView;
		m_ZoomEdit.SetView(m_pView);
		m_ZoomOut.SetView(m_pView);
		m_ZoomSlider.SetView(m_pView);
		m_ZoomIn.SetView(m_pView);
		m_Background.SetView(m_pView);
		Invalidate();
	}
}

CSize DisplayPanel::GetMinWindowSize() const
{
	CDC * pDC = CDC::FromHandle(::GetDC(0));
	CFont * pFont = const_cast<CFont *>(Font()), * pOldFont;
	pOldFont = pDC->SelectObject(pFont);
	CSize s = pDC->GetTextExtent("A");
	s.cy += 4;
	s.cx = 125;
	pDC->SelectObject(pOldFont);
	ReleaseDC(0, pDC->m_hDC);
	return s;
}

CRect DisplayPanel::Bounds() const
{
	if (m_Frame.m_hWnd == 0)
		return CRect(0, 0, 0, 0);

	CRect r = m_Frame.GetClientRect();
	if (m_Frame.IsStatusBarVisible())
		r.bottom -= StatusHeight;
	if (m_Frame.m_ToolBar.Visible())
		r.left += m_Frame.m_ToolBar.Width();
	if (m_Frame.m_RightBar.Visible())
		r.right -= m_Frame.m_RightSplit;
	r.right -= SplitterSize;
	if (m_Frame.m_BottomBar.Visible())
		r.bottom -= m_Frame.m_BottomSplit;
	r.bottom -= SplitterSize;

	CSize s = GetMinWindowSize();
	r.top = r.bottom - s.cy;

	return r;
}

bool DisplayPanel::Visible() const
{
	return true;
}

void DisplayPanel::HandlePaint(CDC * pDC, const CRect & r)
{
	SaveObjects save(pDC);

	CRect rClient = ClientRect();

	COLORREF cF = GetSysColor(COLOR_BTNFACE);
	CBrush bFace(cF);
	pDC->FillRect(&rClient, &bFace);

	COLORREF cL = RGB(255, 255, 255);
	COLORREF cS = GetSysColor(COLOR_BTNSHADOW);

	pDC->Draw3dRect(&rClient, cL, cS);
}

void DisplayPanel::LayoutControls()
{
	CRect rClient = ClientRect();
	int w = rClient.Width();
	int wZoom = 175;
	if (w < wZoom)
		wZoom = w;
	if (wZoom < 150)
		wZoom = 150;

	CRect r(rClient);
	r.left = 0;
	r.right = r.left + 75;
	r.DeflateRect(-1, -1);
	m_ZoomEdit.SetBounds(r);
	r.InflateRect(-1, -1);
	r.left = r.right;
	r.right = r.left + 25;
	m_ZoomOut.SetBounds(r);
	r.left = r.right;
	r.right = r.left + wZoom - 125;
	m_ZoomSlider.SetBounds(r);
	r.left = r.right;
	r.right = r.left + 25;
	m_ZoomIn.SetBounds(r);

	r.left = r.right;
	r.right = r.left + r.Height();
	m_Background.SetBounds(r);

	for (My::Size i = 0; i < m_ExtraWidgets.size(); ++i) {
		Widget * pWidget = m_ExtraWidgets[i];
		if (!pWidget->Visible())
			continue;
		CSize s = pWidget->GetPreferredSize();
		r.left = r.right;
		r.right = r.left + s.cx;
		pWidget->SetBounds(r);
	}


}

void DisplayPanel::AddExtraWidget(Widget * pWidget)
{
	m_ExtraWidgets.push_back(pWidget);
	Add(pWidget);
	LayoutControls();
}


} // My